<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">


    <title>模块化地图</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
    <style type="text/css">
        html,body,#container{
            height:100%;
        }
        .info{
            width:16rem;
        }
    </style>
</head>
<body>
<div id="container"></div>

<div class="input-card">
    <h4>行政区查询</h4>
    <div class="info">
        <div class="box box-default">
            <div class="box-header with-border">
                <h4>辖区基本信息</h4><hr>
            </div><!-- /.box-header -->
            <div class="box-body">
                <div class="input-item">
                    <div class="input-item-prepend" style="width: 60%"><span class="input-item-text" style="width: 90%">下级流行区数量</span></div>
                    <input style="width:80px;text-align: center" value="160" readonly="readonly"></input>
                </div>
                <div class="input-item">
                    <div class="input-item-prepend" style="width: 60%"><span class="input-item-text" style="width: 90%">下级管理员数量</span></div>
                    <input style="width:80px;text-align: center" value="160" readonly="readonly"></input>
                </div>
                <div class="input-item">
                    <div class="input-item-prepend" style="width: 60%"><span class="input-item-text" style="width: 90%">犬数量</span></div>
                    <input style="width:80px;text-align: center" value="160" readonly="readonly"></input>
                </div>
                <div class="input-item">
                    <div class="input-item-prepend" style="width: 60%"><span class="input-item-text" style="width: 90%">佩戴项圈犬数量</span></div>
                    <input style="width:80px;text-align: center" value="160" readonly="readonly"></input>
                </div>
                <div class="input-item">
                    <div class="input-item-prepend" style="width: 60%"><span class="input-item-text" style="width: 90%">智能驱虫总次数</span></div>
                    <input style="width:80px;text-align: center" value="160" readonly="readonly"></input>
                </div>
                <div class="input-item">
                    <div class="input-item-prepend" style="width: 60%"><span class="input-item-text" style="width: 90%">使用硬件驱虫比</span></div>
                    <input style="width:80px;text-align: center" value="160" readonly="readonly"></input>
                </div>
            </div><!-- /.box-body -->
        </div><!-- /.box -->




        <!--<h4>辖区基本信息</h4><hr>-->
        <!--&lt;!&ndash;<h4 id='status'></h4><hr>&ndash;&gt;-->
        <!--&lt;!&ndash;<p id='result'></p><hr>&ndash;&gt;-->
        <!--<div class="input-item">-->
            <!--<p>下级流行区数量: </p>-->
            <!--<span style="align: right; color: red">160</span>-->
        <!--</div>-->
        <!--<div class="input-item">-->
            <!--<p>下级管理员数量: </p>-->
            <!--<span style="align: right; color: red">160</span>-->
        <!--</div>-->
        <!--<div class="input-item">-->
            <!--<p>犬数量: </p>-->
            <!--<span style="align: right; color: red">160</span>-->
        <!--</div>-->
        <!--<div class="input-item">-->
            <!--<p>佩戴项圈犬数量: </p>-->
            <!--<span style="align: right; color: red">160</span>-->
        <!--</div>-->
        <!--<div class="input-item">-->
            <!--<p>智能驱虫总次数: </p>-->
            <!--<span style="align: right; color: red">160</span>-->
        <!--</div>-->
        <!--<div class="input-item">-->
            <!--<p>使用硬件驱虫比: </p>-->
            <!--<span style="align: right; color: red">160</span>-->
        <!--</div>-->
     </div>

    <div class="input-item">
        <div class="input-item-prepend"><span class="input-item-text" >省市区</span></div>
        <select id='province' style="width:100px" onchange='search(this)'></select>
    </div>
    <div class="input-item">
        <div class="input-item-prepend"><span class="input-item-text" >地级市</span></div>
        <select id='city' style="width:100px" onchange='search(this)'></select>
    </div>
    <div class="input-item">
        <div class="input-item-prepend"><span class="input-item-text" >区县</span></div>
        <select id='district' style="width:100px" onchange='search(this)'></select>
    </div>
    <div class="input-item">
        <div class="input-item-prepend"><span class="input-item-text" >街道</span></div>
        <select id='street' style="width:100px" onchange='setCenter(this)'></select>
    </div>
</div>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.12&key=a95f2a626d3afd67fb6ae4231c8997d5&plugin=AMap.DistrictSearch"></script>
<script type="text/javascript">
    var map, district, polygons = [], citycode;
    var citySelect = document.getElementById('city');
    var districtSelect = document.getElementById('district');
    var areaSelect = document.getElementById('street');

    map = new AMap.Map('container', {
        resizeEnable: true,
//        center: [116.30946, 39.937629],
        zoom: 3
    });
    //行政区划查询
    var opts = {
        subdistrict: 1,   //返回下一级行政区
        showbiz:false  //最后一级返回街道信息
    };
    district = new AMap.DistrictSearch(opts);//注意：需要使用插件同步下发功能才能这样直接使用
    district.search('中国', function(status, result) {
        if(status=='complete'){
            getData(result.districtList[0]);
        }
    });
    function getData(data,level) {
        var bounds = data.boundaries;
        if (bounds) {
            for (var i = 0, l = bounds.length; i < l; i++) {
                var polygon = new AMap.Polygon({
                    map: map,
                    strokeWeight: 1,
                    strokeColor: '#0091ea',
                    fillColor: '#80d8ff',
                    fillOpacity: 0.2,
                    path: bounds[i]
                });
                polygons.push(polygon);
            }
            map.setFitView();//地图自适应
        }

        //清空下一级别的下拉列表
        if (level === 'province') {
            citySelect.innerHTML = '';
            districtSelect.innerHTML = '';
            areaSelect.innerHTML = '';
        } else if (level === 'city') {
            districtSelect.innerHTML = '';
            areaSelect.innerHTML = '';
        } else if (level === 'district') {
            areaSelect.innerHTML = '';
        }

        var subList = data.districtList;
        if (subList) {
            var contentSub = new Option('--请选择--');
            var curlevel = subList[0].level;
            var curList =  document.querySelector('#' + curlevel);
            curList.add(contentSub);
            for (var i = 0, l = subList.length; i < l; i++) {
                var name = subList[i].name;
                var levelSub = subList[i].level;
                var cityCode = subList[i].citycode;
                contentSub = new Option(name);
                contentSub.setAttribute("value", levelSub);
                contentSub.center = subList[i].center;
                contentSub.adcode = subList[i].adcode;
                curList.add(contentSub);
            }
        }

    }
    function search(obj) {
        //清除地图上所有覆盖物
        for (var i = 0, l = polygons.length; i < l; i++) {
            polygons[i].setMap(null);
        }
        var option = obj[obj.options.selectedIndex];
        var keyword = option.text; //关键字
        var adcode = option.adcode;
        district.setLevel(option.value); //行政区级别
        district.setExtensions('all');
        //行政区查询
        //按照adcode进行查询可以保证数据返回的唯一性
        district.search(adcode, function(status, result) {
            if(status === 'complete'){
                getData(result.districtList[0],obj.id);
            }
        });
    }
    function setCenter(obj){
        map.setCenter(obj[obj.options.selectedIndex].center)
    }


    AMap.plugin('AMap.Geolocation', function() {
        var geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,//是否使用高精度定位，默认:true
            timeout: 10000,          //超过10秒后停止定位，默认：5s
            buttonPosition:'LB',    //定位按钮的停靠位置
            buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
            zoomToAccuracy: true,   //定位成功后是否自动调整地图视野到定位点

        });
        map.addControl(geolocation);
        geolocation.getCurrentPosition(function(status,result){
            if(status=='complete'){
//                onComplete(result)
                alert("定位成功");
            }else{
//                onError(result)
                alert("定位失败");
            }
        });
    });


    //解析定位结果
    function onComplete(data) {
        document.getElementById('status').innerHTML='定位成功'
        var str = [];
        str.push('定位结果：' + data.position);
        str.push('定位类别：' + data.location_type);
        if(data.accuracy){
            str.push('精度：' + data.accuracy + ' 米');
        }//如为IP精确定位结果则没有精度信息
        str.push('是否经过偏移：' + (data.isConverted ? '是' : '否'));
        document.getElementById('result').innerHTML = str.join('<br>');
    }
    //解析定位错误信息
    function onError(data) {
        document.getElementById('status').innerHTML='定位失败'
        document.getElementById('result').innerHTML = '失败原因排查信息:'+data.message;
    }
</script>
<script type="text/javascript" src="https://webapi.amap.com/demos/js/liteToolbar.js"></script>

</body>
</html>